package com.uenpay.im.ui.old.activity.chat;

import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.qiyunxin.android.http.utils.FileUtils;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.im.adapter.DownloadedFileAdapter;
import com.uenpay.im.entities.SendFile;
import com.uenpay.util.app.base.BaseActivity;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Locale;

/**
 * 下载文件列表
 *
 * @author SL
 * @copyRight http://www.qiyunxin.com
 */
public class DownloadedFileListActivity extends BaseActivity {
    private ListView mListview;
    private TextView send_file_btn;
    private SendFile mSendFile;
    private DownloadedFileAdapter adapter;
    private LinearLayout no_download_file_layout;
    private Calendar mCalendar = Calendar.getInstance();
    private SimpleDateFormat formatter = null;
    private FileUtils fileUtils = new FileUtils(IMApplication.appName);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isFullScreen = true;
        super.onCreate(savedInstanceState);
    }

    @Override
    public View bindView() {
        return null;
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_download_file_list;
    }

    @Override
    public void initView(View view) {
        formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
                Locale.getDefault());
        no_download_file_layout = (LinearLayout) findViewById(R.id.no_download_file_layout);
        mListview = (ListView) findViewById(R.id.download_file_list);
        ((TextView) findViewById(R.id.title_textview)).setText(getResources()
                .getString(R.string.choose_download_file));
        findViewById(R.id.title_right_layout).setVisibility(View.VISIBLE);
        send_file_btn = (TextView) findViewById(R.id.title_right_tv);
        send_file_btn.setText(getResources().getString(R.string.send_file));
        send_file_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mSendFile == null) {
                    return;
                }
                /** 将选中的文件返回给会话页面 **/
                Intent data = new Intent();
                data.putExtra("send_file", mSendFile);
                setResult(RESULT_OK, data);
                DownloadedFileListActivity.this.finish();
            }
        });
        mListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                mSendFile = (SendFile) mListview.getItemAtPosition(position);
                adapter.setSelectFile(position);
            }
        });
        String path = Environment.getExternalStorageDirectory()
                .getAbsolutePath() + fileUtils.getFileDownloadPath();
        ArrayList<SendFile> list = new ArrayList<SendFile>();
        try {
            File download_file = new File(path);
            File[] files = download_file.listFiles();
            if (files == null || files.length < 1) {
                no_download_file_layout.setVisibility(View.VISIBLE);
                send_file_btn.setVisibility(View.GONE);
                return;
            }
            for (int i = 0, lenght = files.length; i < lenght; i++) {
                SendFile mFile = new SendFile();
                mFile.filePath = files[i].getAbsolutePath();
                mFile.fileName = files[i].getName();
                mFile.fileSize = mFile.getFileSize(files[i].length() + "");
                mCalendar.setTimeInMillis(files[i].lastModified());
                mFile.fileAddDate = formatter.format(mCalendar.getTime());
                list.add(mFile);
            }
        } catch (Exception e) {
        }
        adapter = new DownloadedFileAdapter(list, this);
        mListview.setAdapter(adapter);
    }
}
